package gqz.testdemo.DragListViewGroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.AbsoluteLayout;
import android.widget.ListView;
import android.widget.TextView;

import gqz.testdemo.R;

/**
 * Created by Administrator on 2018/8/16.
 */

public class DragListViewGroup extends AbsoluteLayout {

    private String TAG = "DragListViewGroup-->";

    private Context context;

    private ListView lv1, lv2;
    private TextView tv;

    private int hSpace = 0;
    private int dragTextSize = 16;
    private int dragTextColor = Color.WHITE;

    public DragListViewGroup(Context context) {
        super(context);
    }

    public DragListViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragListViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DragListViewGroup, 0, 0);
        hSpace = ta.getDimensionPixelSize(R.styleable.DragListViewGroup_hSpace, 0);
        dragTextSize = ta.getDimensionPixelSize(R.styleable.DragListViewGroup_dragTextSize, dragTextSize);
        dragTextColor = ta.getColor(R.styleable.DragListViewGroup_dragTextColor, dragTextColor);

        lv1 = new ListView(context);
        lv2 = new ListView(context);
        tv = new TextView(context);

        lv1.setBackgroundColor(Color.parseColor("#ccc"));
        lv2.setBackgroundColor(Color.parseColor("#aaa"));

        tv.setText("Normal Text");
        tv.setTextColor(dragTextColor);
        tv.setTextSize(dragTextSize);

        addView(lv1);
        addView(lv2);
        addView(tv);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() == 0) {//如果没有子View,当前ViewGroup没有存在的意义，不用占用空间
            setMeasuredDimension(0, 0);
            return;
        }

        int w = getMeasureSize(widthMeasureSpec, 500);
        int h = getMeasureSize(heightMeasureSpec, 400);

        setMeasuredDimension(w, h);
    }

    private int getMeasureSize(int measureSpec, int defaultSize) {
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
                return defaultSize;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                return size;
            default:
                return defaultSize;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
